package controler.states;
import controler.A_State;
import controler.userCommands.AddNewElement;
import controler.userCommands.SwitchState;
import flash.events.Event;
import graphic.RenderObject;
import graphic.RessourceManager;
import model.LevelEditorModelEvent;
import model.ModelUpdateType;
import view.events.LevelEditorViewKeyboardEvent;
import view.events.LevelEditorViewMouseEvent;
import view.events.LevelEditorViewOtherEvent;

/**
 * @author Damien Mabin
 */

class CreateRenderObject extends A_State
{
	private var m_RenderObjectToClone 	: RenderObject;
	private var m_TempoName				: String;
	private var m_TempoFile				: String;
	
	public function new() 
	{
		super();
		
		m_RenderObjectToClone = null;
		m_TempoName = null;
		m_TempoFile = null;
			
		this.addEventListener(LevelEditorViewMouseEvent.EVENT, OnLevelEditorMouseEvent);
		this.addEventListener(LevelEditorViewOtherEvent.EVENT, OnLevelEditorViewOtherEvent);
		this.addEventListener(LevelEditorViewKeyboardEvent.EVENT, OnLevelEditorKeyboardEvent);
	}
	
	public function SetRenderObjectToClone(_Name:String, _File:String):Void
	{
		if (_Name != null && _File != null)
		{
			var ressourceManager : RessourceManager = RessourceManager.GetInstance();
			ressourceManager.addEventListener(RessourceManagerEvent.PREPARATION_COMPLETE, OnPreparationComplete);
			ressourceManager.PrepareRenderObjects([ { modelName : _Name, file : _File } ]);
			m_TempoFile = _File;
			m_TempoName = _Name;
		}
		else
		{
			m_RenderObjectToClone = null;
			m_TempoName = null;
			m_TempoFile = null;
			A_State.s_View.dispatchEvent(new LevelEditorModelEvent(E_ModelUpdateType.PREPARE_RENDER_OBJECT_CREATION(null, null)));
			A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.INITIAL));
		}
	}
	
	private function OnLevelEditorKeyboardEvent(_Evt:LevelEditorViewKeyboardEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case KEY_DOWN :
			{
			}
			case KEY_UP :
			{
				if (_Evt.m_KeyCode == 17 /*CTRL_KEY*/)
				{
					A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.INITIAL));
					A_State.s_View.dispatchEvent(new LevelEditorModelEvent(E_ModelUpdateType.PREPARE_RENDER_OBJECT_CREATION(null, null)));
				}
			}
		}
	}
	
	private function OnPreparationComplete(_Evt:Event):Void
	{
		RessourceManager.GetInstance().removeEventListener(RessourceManagerEvent.PREPARATION_COMPLETE, OnPreparationComplete);
		m_RenderObjectToClone = RessourceManager.GetInstance().InstanciateRenderObject(m_TempoName);
		m_RenderObjectToClone.name = "NewRenderObject";
		A_State.s_View.dispatchEvent(new LevelEditorModelEvent(E_ModelUpdateType.PREPARE_RENDER_OBJECT_CREATION(m_TempoName, m_TempoFile)));
	}
	
	private function OnLevelEditorViewOtherEvent(_Evt:LevelEditorViewOtherEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case SELECT_RENDER_OBJECT_FOR_CREATION(name, file) :
			{
				SetRenderObjectToClone(name, file);
			}
			default : 
			{
				
			}
		}
	}
	
	private function OnLevelEditorMouseEvent(_Evt:LevelEditorViewMouseEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case MOUSE_DOWN :
			{
				if (m_RenderObjectToClone != null)
				{
					var roToCreate : RenderObject = m_RenderObjectToClone.Clone();
					roToCreate.SetPosition(_Evt.m_MousePosition.x, _Evt.m_MousePosition.y);
					if (A_State.s_Controller.m_KeyboardKeyState[17/*Ctrl*/])
					{
						A_State.s_Controller.DoUserCommand(new AddNewElement(null, RenderObject, roToCreate));
						A_State.s_View.dispatchEvent(new LevelEditorModelEvent(E_ModelUpdateType.PREPARE_RENDER_OBJECT_CREATION(m_TempoName, m_TempoFile)));
					}
					else
					{
						A_State.s_Controller.DoUserCommand(new AddNewElement(A_State.s_Controller.INITIAL, RenderObject, roToCreate));
						A_State.s_View.dispatchEvent(new LevelEditorModelEvent(E_ModelUpdateType.PREPARE_RENDER_OBJECT_CREATION(null, null)));
					}
				}
			}
			case MOUSE_MOVE :
			{
				
			}
			case MOUSE_UP :
			{
				
			}
			case MOUSE_WHEEL :
			{
				
			}
		}
	}
	
}